package com.xiaolin.jpademo.service.impl;

import com.xiaolin.jpademo.domain.PpDocumentEntity;
import com.xiaolin.jpademo.dto.PpDocumentQueryParam;
import com.xiaolin.jpademo.repository.PpDocumentRepository;
import com.xiaolin.jpademo.service.PpDocumentService;
import com.xiaolin.jpademo.utils.PageUtils;
import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName PpDocumentServiceImpl
 * @Description 描述
 * @Detail detail
 * @Author MyPC
 * @Date 2021/1/10
 * @Version 1.0
 */
@Service
public class PpDocumentServiceImpl implements PpDocumentService{
    @Autowired
    private PpDocumentRepository ppDocumentRepository;

    /**
     * 分页条件查询
     * @param param
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<PpDocumentEntity> queryList(PpDocumentQueryParam param, Integer pageNum, Integer pageSize) {
        // 1、条件查询
        ExampleMatcher matcher=ExampleMatcher.matching().
                withMatcher("title", ExampleMatcher.GenericPropertyMatchers.startsWith());  //模糊匹配，不指定就默认是等值匹配
        PpDocumentEntity entity=new PpDocumentEntity();
        entity.setTitle(param.getTitle());
        entity.setCategoryId(param.getCategoryId());
        Example<PpDocumentEntity> example= Example.of(entity,matcher);
        // 2、分页+ 排序 查询
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable=PageRequest.of(pageNum, pageSize,sort);
        // 3、封装分页对象
        return PageUtils.getPageObj(ppDocumentRepository.findAll(example,pageable));
    }
}
